﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using SzCourt.Logic.BLL;
using SzCourt.Logic.NBL.DBEntities;


namespace SzCourt.WebSite.lian
{    
    public partial class success : System.Web.UI.Page
    {
        private int fileMaxSize = 1024 * 1024 * 10;
        private string _AppContent = string.Empty;
        private string _PreCaseInfo = string.Empty;
        private string _CauseOfActionID = string.Empty;
        private string _CaseFiles = string.Empty;
        pPreCaseInfo mPreCaseInfo = new pPreCaseInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                getParam();
                createrInfo();
            }
        }

        //获取参数值
        private void getParam()
        {
            this._AppContent = string.IsNullOrEmpty(Request.Form["AppContent"]) ? "" : Request.Form["AppContent"];

            this._CauseOfActionID = string.IsNullOrEmpty(Request.Form["hdnCauseOfActionID"]) ? "1" : Request.Form["hdnCauseOfActionID"];
            this._PreCaseInfo = string.IsNullOrEmpty(Request.Form["PreCaseInfo"]) ? "" : Request.Form["PreCaseInfo"];
            this._CaseFiles = string.IsNullOrEmpty(Request.Form["PreCaseFile"]) ? "" : Request.Form["PreCaseFile"];
            string[] aCaseInfo = this._PreCaseInfo.Split('~');
            mPreCaseInfo.QueryPWD = aCaseInfo[0];
            mPreCaseInfo.Email = aCaseInfo[1];
            mPreCaseInfo.Phone = aCaseInfo[2];
            mPreCaseInfo.PreCaseType = short.Parse(aCaseInfo[3]);
            mPreCaseInfo.CauseOfActionID = int.Parse(this._CauseOfActionID);
        }

        private void createrInfo()
        {
            PreCassCollection param = new PreCassCollection();

            List<pPreParties> mPartiesList = new List<pPreParties>();
            string[] aApps = this._AppContent.Split('$');//切分为原被告
            for (int a = 0; a < aApps.Length; a++)
            {
                string[] alis = aApps[a].Split( '~' );//切分有多少个原被告
                for (int l = 1; l < alis.Length; l++)
                {
                    string[] aentity = alis[l].Split('&' );//切分出每一个元素
                    pPreParties tmpPP = new pPreParties();
                    tmpPP.PartiesType = short.Parse(aentity[0]); 
                    tmpPP.PartiesClass = short.Parse(aentity[1]);
                    tmpPP.PartiesName = aentity[2];

                    switch (aentity[1])
                    {
                        case "1":
                            tmpPP.IdentityCard = aentity[3];
                            tmpPP.Birth = aentity[4];
                            tmpPP.Nationaly = aentity[5];
                            tmpPP.National = aentity[6];
                            tmpPP.Political = aentity[7];
                            tmpPP.Degree = aentity[8];
                            tmpPP.Phone1 = aentity[9];
                            tmpPP.PostalCode = aentity[10];
                            tmpPP.Domicile = aentity[11];
                            tmpPP.Address = aentity[12];
                            break;

                        case "2":
                            tmpPP.LegalPerson = aentity[3];
                            tmpPP.Phone1 = aentity[4];
                            tmpPP.PostalCode = aentity[5];
                            tmpPP.Domicile = aentity[6];
                            tmpPP.Address = aentity[7];
                            break;
                        case "3":
                            tmpPP.LegalPerson = aentity[3];
                            tmpPP.Phone1 = aentity[4];
                            tmpPP.PostalCode = aentity[5];
                            tmpPP.Domicile = aentity[6];
                            tmpPP.Address = aentity[7];
                            break;
                    }
                    mPartiesList.Add(tmpPP);  
                }
            }

            List<pPreCaseFile> mFileList = new List<pPreCaseFile>();
             //string[] mfiles = this._AppContent.Split('~');//切分为每个文件
             //for (int a = 0; a < mfiles.Length; a++)
             {
                 pPreCaseFile casefile = new pPreCaseFile();
                 casefile.FileName = this._AppContent;
                 mFileList.Add(casefile);
                 
             }
            //当事人列表
            param.PrePartiesList = mPartiesList;

            //初始化基本信息
            param.PreCaseInfo = mPreCaseInfo;

            //文件列表
            param.PreCaseFile = mFileList;

            PreCase bll = new PreCase();
            string result = (string)bll.CreatePreCase(param);
            doUpload();
            if (result.Length>0)
            {
                this.spMessage.InnerHtml = "您的立案申请已经成功!<br />请您一定要记住立案编号：" + result + "和查询密码：" + mPreCaseInfo .QueryPWD+ "，以便查询立案的状态。";
            }
            else
            {
                this.spMessage.InnerHtml = "立案申请失败，请检查信息是否填写正确！";
            }
        }

        protected void doUpload()
        {
            try
            {

                //获取上传的文件

                HttpPostedFile file = (HttpPostedFile)Request.Files["fileUpload"];                

                //判断文件大小
                if (file.ContentLength > this.fileMaxSize)//file.FileSize > this.fileMaxSize
                {
                    throw new Exception("文件超过允许的大小！");
                }
                string tempDir = "/file/yulian/";
                string urlPath = tempDir + file.FileName;

                if (!Directory.Exists(Server.MapPath(tempDir)))
                {
                    Directory.CreateDirectory(Server.MapPath(tempDir));
                }                
                file.SaveAs(Server.MapPath(urlPath));            

            }
            catch (Exception ex)
            {
                //WriteJs("objUploadFile.uploaderror('" + ex.Message + "');");
            }
        }

    }
}